package com.audaque.springboot.foshanupload.web.datamongodbdemo.controller;

import com.audaque.springboot.foshanupload.web.datamongodbmodeldemo.model.pojo.Cat;
import com.audaque.springboot.foshanupload.web.datamongodbdemo.service.impl.CatOperations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zgb
 * @desc ...
 * @date 2023-12-05 23:29:50
 */
@RestController
@RequestMapping("/catOperation")
public class CatOperationsController {


    @Autowired
    private CatOperations catOperations;


    @PostMapping("/insert")
    public void insert() throws Exception {
        List<String> hobbyList = new ArrayList<String>();
        hobbyList.add("玩毛线球");
        hobbyList.add("凝望");
        hobbyList.add("吃小鱼干");
        Cat cat = Cat.builder().name("小咪")
                .sex("female")
                .age(1)
                .hobbyList(hobbyList)
                .createTime(new Date())
                .master("刘忽悠")
                .build();
        catOperations.insert(cat);
    }

    @PostMapping("/batchInsert")
    public void batchInsert() throws Exception {
        List<Cat> cats = new ArrayList<>();
        List<String> hobbyList = new ArrayList<>();
        hobbyList.add("玩毛线球");
        hobbyList.add("看电视");
        hobbyList.add("凝望");
        hobbyList.add("吃小鱼干");
        Cat cat = Cat.builder().name("大咪")
                .sex("female")
                .age(5)
                .hobbyList(hobbyList)
                .createTime(new Date())
                .master("刘小皮")
                .build();
        cats.add(cat);
        Cat cat1 = Cat.builder().name("阿飞")
                .sex("female")
                .age(3)
                .hobbyList(hobbyList)
                .createTime(new Date())
                .master("刘傻瓜")
                .build();
        cats.add(cat1);
        catOperations.batchInsert(cats);
    }

    @GetMapping("/queryByName")
    public List<Cat> queryByName() throws Exception {
       return catOperations.queryByName("小咪");
    }

    @GetMapping("/queryByNameAndAge")
    public List<Cat> queryByNameAndAge() throws Exception {
        return catOperations.queryByNameAndAge("阿飞", 3);

    }

    @GetMapping("/queryByHobbyList")
    public  List<Cat> queryByHobbyList() throws Exception {
        List<String> hobbyList = new ArrayList<String>();
        hobbyList.add("玩毛线球");
        hobbyList.add("凝望");
        hobbyList.add("吃小鱼干");
       return catOperations.queryByHobbyList(hobbyList);

    }

    @GetMapping("/orderByCreateTime")
    public  List<Cat> orderByCreateTime() throws Exception {
      return catOperations.orderByCreateTime();

    }

    @GetMapping("/queryByAgeRange")
    public List<Cat> queryByAgeRange() throws Exception {
        return catOperations.queryByAgeRange(2, 5);
    }

    @DeleteMapping ("/deleteAll")
    public void deleteAll() throws Exception {
        catOperations.deleteAll();
    }


}
